/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. All Rights Reserved.                             */
/* Open Source Software - may be modified and shared by FRC teams. The code   */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project.                                                               */
/*----------------------------------------------------------------------------*/

package com.nurdrobotics;

import edu.wpi.first.wpilibj.Joystick;

/**
 * 
 */
public class DriverStick {
    // The following variables gives a name to the port that is connected
    // to the driver joystick.
    // This way, it's easy to look at this section of code to make sure that
    // the driver joystick is configured correctly in the DriverStation
    private static final int PORT_DRIVER = 1;

    // The following variables define names for the axes on the driver
    // joystick
    private static final int AXIS_MOVE = 2;
    private static final int AXIS_ROTATE = 1;

    // The following variables define names for all the buttons on the
    // driver joystick
    private static final int BUTTON_BOOST = 8;
    private static final int BUTTON_RESET_COUNTER = 9;

    // declare an variable for holding the joystick
    private static Joystick m_joystick = null;

    /*
     * This function initializes variables in this class. It should
     * be called exactly once prior to using the class.
     */
    public static boolean init() {
        // initialize the joystick
        m_joystick = new Joystick(PORT_DRIVER);
        if(m_joystick == null) {
            return false;
        }

        return true;
    }

    /*
     * Returns true if the boost button is pressed, false otherwise
     */
    public static boolean boostPressed() {
        // check to see whether we should open or close the claw
        return(m_joystick.getRawButton(DriverStick.BUTTON_BOOST));
    }

    /*
     * If the boost button is not pressed, this routine scales the supplied
     * value down to a number between 0 and a maximum power value.
     * If the boost button is pressed, this routine simply returns the
     * number that was supplied.
     */
    private static double getBoostScaledValue(double value) {
        // if the boost button is not pressed, scale the move and rotate
        // values by the boost factor
        if (DriverStick.boostPressed() == false) {
            // previously we had the scale factor tied to a slider,
            // but we want to use the sliders for other things so now
            // this is a constant
            double powerFactor = 0.8;

            // Scale the value
            // For example, if the value is 1, and boost is not pressed,
            // and the boost gain slider is 0.8, then the new value is
            // 1 * .8 = .8
            // If the value is 0.5 and the boost gain slider is .8, then
            // the new value is:
            // .5 * .8 = .4
            value = value * powerFactor;
        }

        return value;
    }

    /*
     * Get the current move value from the joystick
     * This reads the value from the correct axis of the joystick that is
     * used for the move value of a robot drive.
     * In addition, if the boost button is not pressed, the value will
     * be scaled down to a number between 0 and the max power setting
     * allowed by an analog slider in the driver station
     */
    public static double getMoveValue() {
        // get the move value from the joystick
        double moveValue = m_joystick.getRawAxis(DriverStick.AXIS_MOVE);

        // return the value scaled according to the boost settings
        return(DriverStick.getBoostScaledValue(moveValue));
    }

    /*
     * Get the current rotate value from the joystick
     * This reads the value from the correct axis of the joystick that is
     * used for the rotate value of a robot drive.
     * In addition, if the boost button is not pressed, the value will
     * be scaled down to a number between 0 and the max power setting
     * allowed by an analog slider in the driver station
     */
    public static double getRotateValue() {
        // get the rotate value from the joystick
        double rotateValue = m_joystick.getRawAxis(DriverStick.AXIS_ROTATE);

        // return the value scaled according to the boost settings
        return(DriverStick.getBoostScaledValue(rotateValue));
    }

    public static boolean ResetCounterPressed() {
        return m_joystick.getRawButton(DriverStick.BUTTON_RESET_COUNTER);
    }
}
